from selenium.webdriver.common.keys import Keys
from base import By, webdriver, sleep

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
driver.maximize_window()

sleep(2)
kw = driver.find_element(By.ID, 'kw')
kw.send_keys('python')
kw.send_keys(Keys.ENTER)

# 滚动到页面底部(行为默认)
sleep(2)
js1 = 'window.scrollTo(0, document.body.scrollHeight)'
driver.execute_script(js1)

# 滚动到页面顶部(行为平滑)
sleep(2)
js2 = 'window.scrollTo({left:0, top:0, behavior:"smooth"})'
driver.execute_script(js2)

# 聚焦到某个元素位置,显示在顶端
sleep(2)
target = driver.find_element(By.XPATH, '//*[@id="2"]/div/div[1]/div[2]/div/div[1]/a[1]/span')
driver.execute_script('arguments[0].scrollIntoView();', target)

sleep(3)
driver.quit()
